/*
  学习目标：使用subscribe监听state变化
*/

import { createStore } from 'redux';

const store = createStore(function (state = { count: 0 }, action) {
  console.warn('进到仓库了, action是  ----->', action);
  if (action.type === 'add') {
    return {
      ...state,
      count: state.count + 1,
    };
  }

  if (action.type === 'des') {
    return {
      ...state,
      count: state.count - 1,
    };
  }
  return state;
});

// console.log('第1次获取state  ----->  ', store.getState());

// 1. 监听变化
// 🔔 返回值是一个解除监听的函数
const unSubFn = store.subscribe(() => {
  console.log('状态发生变化了, state的值  ----->  ', store.getState());
});

store.dispatch({ type: 'add' });
store.dispatch({ type: 'des' });
// // 2. 关闭监听
unSubFn();
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
